package com.fr.schedule.task;

import com.fr.base.ReflectionUtils;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.report.parameter.Parameter;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.IdFieldMapper;
import com.fr.schedule.dao.JdbcDaoManager;
import com.fr.schedule.dao.ObjectMapper;
import com.fr.schedule.dao.ValueFieldMapper;
import com.fr.schedule.output.ScheduledOutput;
import com.fr.schedule.trigger.ITrigger;
import com.fr.third.org.quartz.Scheduler;
import com.fr.third.org.quartz.SchedulerException;
import com.fr.third.org.quartz.Trigger;
import com.fr.web.platform.TransmitParameters;
import com.fr.web.platform.entry.ReportletEntry;
import java.util.Date;

public class ScheduleTask
{
  public static final String TABLE_NAME = "fr_schedule_task";
  private long id = -1L;
  private long groupId;
  private String name;
  private String description;
  private ITrigger iTrigger;
  private TransmitParameters taskParameters;
  private ScheduledOutput scheduledOutput;
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_schedule_task", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("groupId", 4, "groupId"), new FieldMapper("name", 12, "name"), new FieldMapper("description", 12, "description"), new IdFieldMapper(ITrigger.class, "iTrigger", 4, "itriggerId")
  {
    public Object id2Field(long paramLong)
    {
      String str = String.valueOf(paramLong);
      if (str.startsWith("1"))
      {
        paramLong = Long.parseLong(str.substring(1));
        return ScheduleContext.getJdbcDaoManager().load((ScheduleTask.class$com$fr$schedule$trigger$OnceITrigger == null) ? (ScheduleTask.class$com$fr$schedule$trigger$OnceITrigger = ScheduleTask.class$("com.fr.schedule.trigger.OnceITrigger")) : ScheduleTask.class$com$fr$schedule$trigger$OnceITrigger, paramLong);
      }
      if (str.startsWith("2"))
      {
        paramLong = Long.parseLong(str.substring(1));
        return ScheduleContext.getJdbcDaoManager().load((ScheduleTask.class$com$fr$schedule$trigger$SimpleITrigger == null) ? (ScheduleTask.class$com$fr$schedule$trigger$SimpleITrigger = ScheduleTask.class$("com.fr.schedule.trigger.SimpleITrigger")) : ScheduleTask.class$com$fr$schedule$trigger$SimpleITrigger, paramLong);
      }
      if (str.startsWith("3"))
      {
        paramLong = Long.parseLong(str.substring(1));
        return ScheduleContext.getJdbcDaoManager().load((ScheduleTask.class$com$fr$schedule$trigger$CalendarITrigger == null) ? (ScheduleTask.class$com$fr$schedule$trigger$CalendarITrigger = ScheduleTask.class$("com.fr.schedule.trigger.CalendarITrigger")) : ScheduleTask.class$com$fr$schedule$trigger$CalendarITrigger, paramLong);
      }
      return null;
    }

    public long field2Id(Object paramObject)
    {
      if (paramObject == null)
        return -1L;
      ScheduleContext.getJdbcDaoManager().saveOrUpdate(paramObject);
      long l = ((Number)ReflectionUtils.getPrivateFieldValue(paramObject, "id")).longValue();
      String str = ((ITrigger)paramObject).getITriggerIdPrefix() + String.valueOf(l);
      return Long.parseLong(str);
    }
  }
  , new ValueFieldMapper("taskParameters", 12, "task_parameters")
  {
    public Object value2Field(Object paramObject)
    {
      String str = (String)paramObject;
      if (StringUtils.isBlank(str))
        return null;
      TransmitParameters localTransmitParameters = null;
      try
      {
        localTransmitParameters = TransmitParameters.parseJSON(new JSONArray(str));
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
      return localTransmitParameters;
    }

    public Object field2Value(Object paramObject)
    {
      if (paramObject == null)
        return "";
      TransmitParameters localTransmitParameters = (TransmitParameters)paramObject;
      String str = "";
      try
      {
        str = localTransmitParameters.createJSON().toString();
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
      return str;
    }
  }
  , new IdFieldMapper(ScheduledOutput.class, "scheduledOutput", 4, "scheduledOupputId") });
  static Class class$com$fr$schedule$trigger$OnceITrigger;
  static Class class$com$fr$schedule$trigger$SimpleITrigger;
  static Class class$com$fr$schedule$trigger$CalendarITrigger;

  public ScheduleTask()
  {
  }

  public ScheduleTask(String paramString)
  {
    this.name = paramString;
  }

  public ScheduleTask(String paramString1, String paramString2)
  {
    this.name = paramString1;
    this.description = paramString2;
  }

  public ScheduleTask(long paramLong, String paramString1, String paramString2, ITrigger paramITrigger, TransmitParameters paramTransmitParameters, ScheduledOutput paramScheduledOutput)
  {
    this.groupId = paramLong;
    this.name = paramString1;
    this.description = paramString2;
    this.iTrigger = paramITrigger;
    this.taskParameters = paramTransmitParameters;
    this.scheduledOutput = paramScheduledOutput;
  }

  public JSONObject createJSONConfig()
    throws JSONException
  {
    Object localObject2;
    JSONObject localJSONObject = new JSONObject();
    if ((getITrigger().getStartTime() != null) && (getITrigger().getStartTime().getTime() > System.currentTimeMillis()))
      localJSONObject.put("state", -2);
    else
      try
      {
        Scheduler localScheduler = ScheduleContext.getScheduler();
        localObject2 = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, getGroupId());
        Trigger[] arrayOfTrigger = localScheduler.getTriggersOfJob(getName(), ((ReportletEntry)localObject2).getDisplayName());
        if (arrayOfTrigger.length > 0)
        {
          Trigger localTrigger = arrayOfTrigger[0];
          int i = localScheduler.getTriggerState(localTrigger.getName(), localTrigger.getGroup());
          localJSONObject.put("state", i);
        }
      }
      catch (SchedulerException localSchedulerException)
      {
        localSchedulerException.printStackTrace();
      }
    localJSONObject.put("id", getId());
    localJSONObject.put("groupId", getGroupId());
    localJSONObject.put("name", getName());
    localJSONObject.put("description", getDescription());
    if (getITrigger() != null)
    {
      localObject1 = getITrigger().createJSONConfig();
      localJSONObject.put("iTrigger", localObject1);
    }
    Object localObject1 = getTaskParameters().createJSON();
    localJSONObject.put("taskParameters", localObject1);
    if (getScheduledOutput() != null)
    {
      localObject2 = getScheduledOutput().createJSONConfig();
      localJSONObject.put("scheduledOutput", localObject2);
    }
    return ((JSONObject)(JSONObject)localJSONObject);
  }

  public boolean appendToReportletEntry(ReportletEntry paramReportletEntry)
  {
    setGroupId(paramReportletEntry.getId());
    return ScheduleContext.getJdbcDaoManager().saveOrUpdate(this);
  }

  public void setId(long paramLong)
  {
    this.id = paramLong;
  }

  public long getId()
  {
    return this.id;
  }

  public void setGroupId(long paramLong)
  {
    this.groupId = paramLong;
  }

  public long getGroupId()
  {
    return this.groupId;
  }

  public String getName()
  {
    return this.name;
  }

  public void setName(String paramString)
  {
    this.name = paramString;
  }

  public void setDescription(String paramString)
  {
    this.description = paramString;
  }

  public String getDescription()
  {
    return this.description;
  }

  public void setITrigger(ITrigger paramITrigger)
  {
    this.iTrigger = paramITrigger;
  }

  public ITrigger getITrigger()
  {
    return this.iTrigger;
  }

  public void setTaskParameters(TransmitParameters paramTransmitParameters)
  {
    this.taskParameters = paramTransmitParameters;
  }

  public TransmitParameters getTaskParameters()
  {
    return this.taskParameters;
  }

  public void setScheduledOutput(ScheduledOutput paramScheduledOutput)
  {
    if ((this.id != 0L) && (this.scheduledOutput != null))
    {
      if ((paramScheduledOutput != null) && (paramScheduledOutput.getId() == this.scheduledOutput.getId()))
        break label72:
      ScheduleContext.getJdbcDaoManager().deleteById(ScheduledOutput.class, this.scheduledOutput.getId());
    }
    label72: this.scheduledOutput = paramScheduledOutput;
  }

  public ScheduledOutput getScheduledOutput()
  {
    return this.scheduledOutput;
  }

  public Parameter[] getParametersToArray()
  {
    return ((getTaskParameters() == null) ? new Parameter[0] : getTaskParameters().toArray());
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof ScheduleTask))
      return false;
    ScheduleTask localScheduleTask = (ScheduleTask)paramObject;
    return ((this.id == localScheduleTask.id) && (this.groupId == localScheduleTask.groupId) && (ComparatorUtils.equals(this.name, localScheduleTask.name)) && (ComparatorUtils.equals(this.description, localScheduleTask.description)) && (ComparatorUtils.equals(this.iTrigger, localScheduleTask.iTrigger)) && (ComparatorUtils.equals(localScheduleTask.getTaskParameters(), getTaskParameters())) && (ComparatorUtils.equals(this.scheduledOutput, localScheduleTask.scheduledOutput)));
  }
}